<?php
/* 
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 */
function viewArtitle() {
    global $DB,$config;
    $DB->query("UPDATE article SET view_count=view_count+1 WHERE link='".$config['link']."'");
    $sql = "SELECT a.id,a.cat_id,c.name as category_name,c.link as cat_link,a.title,a.image,a.short_description,a.description,a.create_date FROM article a LEFT JOIN article_category c ON a.cat_id=c.id WHERE a.link='".$config['link']."'";
    $rsArticle = $DB->query_first($sql);
    $config['link'] = $rsArticle['cat_link'];
    if($rsArticle) {
        return $rsArticle;
    }else {
        header("Location: ".$config['domain']);
    }

}
function viewAttachFiles() {
    global $DB,$config;
    $rsId = $DB->query_first("SELECT id FROM article WHERE link='".$config['link']."'");
    if($rsId) {
        $rsAttachFiles = $DB->query("SELECT id,real_filename,filename FROM article_attach WHERE article_id=".$rsId['id']);
        return $rsAttachFiles;
    }
}
function getTagByArticleId($id) {
    global $DB,$config;
    $rsTagsId = $DB->query("SELECT tag_id FROM article_tags WHERE article_id=".intval($id));
    $totalTagsId = $DB->num_rows($rsTagsId);
    $e=0;$strTagsId='';
    while($rs=$DB->fetch_array($rsTagsId)) {
        $e++;
        if($e == $totalTagsId) {
            $strTagsId .= $rs['tag_id'];
        }else {
            $strTagsId .= $rs['tag_id'].',';
        }
    }
    if($strTagsId!='') {
        $rsTags = $DB->query("SELECT tags,tags_link FROM tags WHERE id IN ($strTagsId)");
        return $rsTags;
    }

}

function get_cat_id_to_arr($parent_id = 0) {
    global $DB;
    if(!$trees) $trees = array();
    $rsCat = $DB->query("SELECT * FROM article_category WHERE  parent_id = ".intval($parent_id));
    if($rsCat) {
        while($rs = $DB->fetch_array($rsCat)) {
            $trees[] = $rs['id'];
        }
    }
    return $trees;
}

function getLastestArticlesByCategoryId($categoryId) {
    global $DB;
    $arrCatId = get_cat_id_to_arr($categoryId);
    $str_cat_id = '';
    for($i=0;$i<count($arrCatId);$i++) {
        if($i==(count($arrCatId)-1)) {
            $str_cat_id .= $arrCatId[$i];
        }else {
            $str_cat_id .= $arrCatId[$i].',';
        }
    }
    if($str_cat_id=='') {
        $sql= "SELECT a.title,a.image,a.link,a.short_description,c.name as cat_name, c.link as cat_link FROM article a LEFT JOIN article_category c
            ON a.cat_id = c.id WHERE a.cat_id=".intval($categoryId)." AND a.status='yes' ORDER BY a.create_date DESC";
    }else {
        $sql= "SELECT a.title,a.image,a.link,a.short_description,c.name as cat_name, c.link as cat_link FROM article a LEFT JOIN article_category c
            ON a.cat_id = c.id WHERE a.cat_id=".intval($categoryId)." OR a.cat_id IN ($str_cat_id) AND a.status='yes' ORDER BY a.create_date DESC";
    }

    $rs = $DB->query_first($sql);
    return $rs;
}

function getArticleByCategoryIdOffsetLimit($categoryId,$offset=0,$limit=10) {
    global $DB;
    $arrCatId = get_cat_id_to_arr($categoryId);
    $str_cat_id = '';
    for($i=0;$i<count($arrCatId);$i++) {
        if($i==(count($arrCatId)-1)) {
            $str_cat_id .= $arrCatId[$i];
        }else {
            $str_cat_id .= $arrCatId[$i].',';
        }
    }
    if($str_cat_id == '') {
        $sql= "SELECT id,title,link FROM article WHERE cat_id=".intval($categoryId)." AND status='yes' ORDER BY id DESC LIMIT $offset,$limit";
    }else {
        $sql= "SELECT id,title,link FROM article WHERE cat_id=".intval($categoryId)." OR cat_id IN ($str_cat_id) AND status='yes' ORDER BY id DESC LIMIT $offset,$limit";
    }

    $rs = $DB->query($sql);
    return $rs;
}

function ortherArticlesNotInCategory($create_date,$categoryId,$limit=10) {
    global $DB,$config;
    if($create_date!='' AND intval($categoryId)>0) {
        $limit = ceil($limit/2);
        $sqlNewer = "SELECT title,link,image,short_description FROM article WHERE create_date > '$create_date' AND cat_id != $categoryId AND status='yes' ORDER BY create_date DESC LIMIT $limit";
        $rsNewer = $DB->query($sqlNewer);
        $sqlOlder = "SELECT title,link,image,short_description FROM article WHERE create_date < '$create_date' AND cat_id != $categoryId AND status='yes' ORDER BY create_date DESC LIMIT $limit";
        $rsOlder = $DB->query($sqlOlder);
        return Array(
        "rsNewer" => $rsNewer,
        "rsOlder" => $rsOlder
        );
    }
}

function ortherNewerArticleInCategory($create_date,$categoryId,$limit=10) {
    if($create_date!='' AND intval($categoryId)>0) {
        global $DB,$config;
        $sql = "SELECT title,link FROM article WHERE create_date > '$create_date' AND cat_id = $categoryId AND status='yes' ORDER BY create_date DESC LIMIT $limit";
        $rs = $DB->query($sql);
        return $rs;
    }
}
function ortherOlderArticleInCategory($create_date,$categoryId,$limit=10) {
    if($create_date!='' AND intval($categoryId)>0) {
        global $DB,$config;
        $sql = "SELECT title,link FROM article WHERE create_date < '$create_date' AND cat_id = $categoryId AND status='yes' ORDER BY create_date DESC LIMIT $limit";
        $rs = $DB->query($sql);
        return $rs;
    }
}

?>
